<?php
namespace Forum\Controller;
use Think\Controller;
class CommonController extends Controller {
    
    
    /*
     * 初始化参数
     */    
    public function _initialize(){
        //加载资源路径
        $this->assign('resource_basic',C("resource_basic"));

        $this->initial_nav_title(CONTROLLER_NAME, CONTROLLER_NAME.ACTION_NAME);
        
        //增加用户的登录判断
        $this->chk_if_login();

    }
    
    /*
     * 管理员的权限判断
     */
    private function chk_if_login() {
        if(!session("?fd_username"))
        {
            $this->error('请先登录',U('Admin/Index/login'));
        }
        
    }

    /*
     * 注销操作
     */
    public function logout() {
        session(null);
        redirect(U('Forum/forum/lists'));
    }

    /*
     * ####################  圈子操作 #######################
     */
    /*
     * 圈子的一些记录性操作
     * @param userid,fid
     */
    public function do_forum_view($userid,$fid) {
        $DB = M("forum_forum_read_record");
        
        $data['fid'] = $fid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_forum_view_count($fid, "views");
            return $id;
        }
        else
        {
            return FALSE;
        }
        
        
        
    }
    
    /*
     * 修改圈子的阅读量 ,收藏次数,分享次数,帖子量,主题量
     * @param 值 views ,favtimes, sharetimes,posts,threads
     */
    public function add_forum_view_count($fid,$param) {
        $DB = M("forum_forum");
        $DB->where("fid=".$fid)->setInc("$param");
        
    }
    
    /*
     * 通过fid获取圈子名称
     * 
     */
    public function get_forum_name_by_fid($fid) {
        $res = M("forum_forum")->find($fid);
        
        return $res['name'];
    }
    
    /*
     * ####################  圈子操作 END #######################
     */
    
    /*
     * ####################  主题操作 #######################
     * 
     * 阅读
     * 点赞
     * 评论
     * 收藏
     * 举报
     * 
     */
    /*
     * 主题的一些记录性操作
     * @param userid,fid
     */
    public function do_thread_view($userid,$tid) {
        $DB = M("forum_threads_read_record");
        
        $data['tid'] = $tid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_thread_param_count($tid, "views");
            return $id;
        }
        else
        {
            return FALSE;
        }
        
    }
    /*
     * 点赞
     */
    public function do_thread_like($userid,$tid) {
        $DB = M("forum_threads_comment_like_record");
        
        $data['tid'] = $tid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_thread_param_count($tid, "liketimes");
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 评论
     */
    public function do_thread_comment($userid,$tid) {
        $DB = M("forum_threads_comment_record");
        
        $data['tid'] = $tid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            
            $this->add_thread_param_count($tid, "replies");
            
            //更新主题的最后操作人信息
            $this->_update_lastpost_info($userid, $tid);
            
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 收藏
     */
    public function do_thread_collection($userid,$tid) {
        $DB = M("forum_threads_collection_record");
        
        $data['tid'] = $tid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_thread_param_count($tid, "favtimes");
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 分享
     */
    public function do_thread_share($userid,$tid) {
        $DB = M("forum_threads_share_record");
        
        $data['tid'] = $tid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_thread_param_count($tid, "sharetimes");
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 举报
     */
    public function do_thread_tipoff($userid,$tid) {
        $DB = M("forum_threads_tipoff_record");
        
        $data['tid'] = $tid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 修改主题的指定参数的量
     */
    public function add_thread_param_count($tid,$param) {
        $DB = M("forum_threads");
        $DB->where("tid=".$tid)->setInc("$param");
        
    }
    
    /*
     * 更新最后的回帖人信息到主题记录
     */
    public function _update_lastpost_info($userid,$tid) {
       $DB = M("forum_threads");
       $DB->tid = $tid;
       
       $DB->lastposttime = I("server.REQUEST_TIME");
       $DB->lastpostuserid = $userid;
       
       $DB->save();
       
       
    }
    
    /*
     * ####################  主题操作 END #######################
     */
    
    
    /*
     * ####################  帖子操作 #######################
     */

    
    
    /*
     * 点赞
     */
    public function do_post_like($userid,$pid) {
        $DB = M("forum_posts_comment_like_record");
        
        $data['pid'] = $pid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_post_param_count($pid, "liketimes");
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 评论
     */
    public function do_post_comment($userid,$pid) {
        $DB = M("forum_posts_comment_record");
        
        $data['pid'] = $pid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            $this->add_post_param_count($pid, "replies");
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    
    /*
     * 举报
     */
    public function do_post_tipoff($userid,$pid) {
        $DB = M("forum_threads_tipoff_record");
        
        $data['pid'] = $pid;
        $data['userid']=$userid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $id = $DB->add($data);
        
        if($id)
        {
            
            return $id;
        }
        else
        {
            return FALSE;
        }
    }
    
    /*
     * 修改帖子的指定参数的量
     */
    public function add_post_param_count($pid,$param) {
        $DB = M("forum_posts");
        $DB->where("pid=".$pid)->setInc("$param");
        
    }
    
    /*
     * 获取帖子的图片
     */
    public function get_post_images($image_str) {
        if(!empty($image_str))
        {
            $imgs= explode(",", $image_str);
            $img_arr = array();
            foreach ($imgs as $key => $value) {
                $img_arr[] = M("forum_images")->find($value);
            }
            
            return $img_arr;
        }
        else
        {
            return "";
        }
        
    }
    
    /*
     * 删除帖子的附件图片
     */
    public function del_post_image($id) {
        $DB = M("forum_images");
        $DB->delete($id);
    }
    
    
    /*
     * ####################  帖子操作END  #######################
     */
    
    /*
     * ####################  获取用户相关资料  #######################
     */
    
    /*
     * 通过userid 获取 昵称
     */
    
    public function get_user_nickname_by_userid($userid) {
        $res = M("forum_users")->find($userid);
        
        return $res['nickname'];
    }

    /**
     * 获取所有用户
     * @return 返回json数据
     */
    public function get_all_user() {
        $DB = M("forum_users");
        $map['invisible']=0;//1隐藏 0 可见 
        $map['isban']=0;//1 禁言 0正常
        $res = $DB->where($map)->select();
        if(!empty($res)){
            foreach ($res as $key => $value) {
                /*过滤字段*/
                unset($res[$key]['level']);
                unset($res[$key]['credit']);
                unset($res[$key]['credit_fix']);
                unset($res[$key]['avatar']);
            }
            $this->ajaxReturn($res,'JSON');

        }
    }

    /**
     * 获取马甲用户
     * @return 返回json数据
     */
    public function get_private_user() {
        $DB = M("forum_users");
        $map['isprivate']=1;//1马甲 0 正常用户 
        $map['invisible']=0;//1隐藏 0 可见 
        $map['isban']=0;//1 禁言 0正常
        $res = $DB->where($map)->select();
        if(!empty($res)){
            foreach ($res as $key => $value) {
                /*过滤字段*/
                unset($res[$key]['level']);
                unset($res[$key]['credit']);
                unset($res[$key]['credit_fix']);
                unset($res[$key]['avatar']);
            }
            $this->ajaxReturn($res,'JSON');

        }
    }
    
    /*
     * ####################  获取用户相关资料 END  #######################
     */
    
    
    
    /**
    * 生成随机数字
    * @return  string
    */
   protected function random_number() {
        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);
        return str_pad(mt_rand(100000, 999999), 6, '0', STR_PAD_LEFT);
    }


    /*
     * 获取一二级标题
     */
    public function initial_nav_title($first_nav,$second_nav) {
        
        if(!empty($first_nav) && !empty($second_nav))
        {
            $this->assign("first_title", C("$first_nav"));
            $this->assign("second_title", C("$second_nav")); 
        }
        
    }
    
    
}